import { Service } from 'egg';

export default class WeixinService extends Service {
    public async getOpenid(code: string): Promise<string> {
        const { secret, appId } = this.ctx.app.config.weixin;
        const url = 'https://api.weixin.qq.com/sns/jscode2session'
            + '?grant_type=authorization_code'
            + `&js_code=${code}`
            + `&secret=${secret}`
            + `&appid=${appId}`;
        const res = await this.ctx.curl(url, { dataType: 'json' });
        this.app.logger.info(res);
        return res.data.openid;
    }
}
